<?
require_once('./lib/authentication.lib.php');
require_once('./lib/navigation.lib.php');
require_once('./lib/sort.lib.php');
?>

<HTML>
<HEAD><TITLE>Top scorers</TITLE>
<SCRIPT TYPE="text/javascript" SRC="js/game.js"></SCRIPT>
<SCRIPT TYPE="text/javascript" SRC="js/sorttable.js"></SCRIPT>
<link rel="stylesheet" href="css/style.css" type="text/css">
<STYLE TYPE="text/css">
<!--
.table_head
   {
   color: white;
   background-color: darkblue;
   }
.table_line
   {
   background-color: lightgrey;
   }
-->
</STYLE>
</HEAD>

<BODY>

<?
$championship = mysql_escape_string($_GET{"id"});
echo "<SCRIPT>\n<!--\nChamp = $championship;\n-->\n</SCRIPT>\n";
?>

<? echo foot_header($championship, "scorers"); ?>

<?

$query = "SELECT * FROM Championship WHERE id = '$championship';";
$result_champ = mysql_query($query);

$num = mysql_numrows($result_champ);

if ($num != 1)
{
	echo "<H1>Invalid championship $championship</H1>";
}

$id = mysql_result($result_champ, 0, "id");
$name = mysql_result($result_champ, 0, "name");
$begin = mysql_result($result_champ, 0, "begin");
$end = mysql_result($result_champ, 0, "end");

$begin = substr($begin, 0, 4);
$end = substr($end, 0, 4);
echo "<h1>$name $begin" . (($begin != $end) ? "/$end" : "") . "</h1>\n";

$query = "SELECT Phase.id FROM Phase where `championship` = $id ORDER BY `order`;";
$result_phase = mysql_query($query);
$num_phase = mysql_numrows($result_phase);
$phases = "''";
for ($j = 0; $j < $num_phase; $j++)
{
	$id_phase = mysql_result($result_phase, $j, "id");
	$phases = "$phases, '$id_phase'";
}

$query = "SELECT Game.id, Player.id, Player.name, count(Goal.id) as goals, sum(if(Goal.penalty = '1', 1, 0)) as penalties, Team.id team_id, Team.name as TeamName FROM Goal, Player, Team, team_player, Game, Phase WHERE Goal.game = Game.id AND Game.phase = Phase.id AND Phase.id IN ($phases) AND Goal.player = Player.id AND team_player.player = Player.id AND team_player.team = Team.id AND team_player.champ = '$championship' AND Goal.team = Team.id AND own_goal = '0' AND Game.played = 'played' GROUP BY Goal.player, Game.id;";
$result_goal = mysql_query($query);
$num_goal = mysql_numrows($result_goal);

echo "<TABLE class='sortable indexed' id='scorers'>\n";
echo "<TR class=table_head><TH align='left'>Player</TH><TH align='left'>Team</TH><TH>Goals</TH><TH>Penalties</TH><TH>Most</TH><TH>Hat Tricks</TH></TR>\n";

for ($i = 0; $i < $num_goal; $i++)
{
	$game_id = mysql_result($result_goal, $i, "Game.id");
	$player_id = mysql_result($result_goal, $i, "Player.id");
	$player = mysql_result($result_goal, $i, "Player.name");
	$team_id = mysql_result($result_goal, $i, "team_id");
	$team = mysql_result($result_goal, $i, "TeamName");
	$goals = mysql_result($result_goal, $i, "goals");
	$penalties = mysql_result($result_goal, $i, "penalties");

	$players[$player_id]["name"] = $player;
	$players[$player_id]["goals"] += $goals;
	$players[$player_id]["penalties"] += $penalties;
	$players[$player_id]["hat_tricks"] += 0;
	if ($goals >= 3) $players[$player_id]["hat_tricks"]++;
	if ($goals > $players[$player_id]["most"]) $players[$player_id]["most"] = $goals;
	$players[$player_id]["teams"][$team_id] = $team;
}

$players = multi_sort($players, array(array("goals", false), "name"));

$count = 0;
while (list($key, $value) = each($players))
{
	echo "<TR class=table_line_";
	echo $count++ % 2 == 0 ? "even" : "odd";
	echo "><TD><A HREF='player.php?champ=$championship&player=$key'>";
	echo $value["name"];
	echo "</A></TD><TD>";
	if (list($team_id, $team) = each($value["teams"]))
	{
		echo "<a href='team.php?champ=$championship&team=$team_id'>$team</a>";
	}
	while (list($team_id, $team) = each($value["teams"]))
	{
		echo ", <a href='team.php?champ=$championship&team=$team_id'>$team</a>";
	}
	echo "</TD><TD align='right'>";
	echo $value["goals"];
	echo "</TD><TD align='right'>";
	echo $value["penalties"];
	echo "</TD><TD align='right'>";
	echo $value["most"];
	echo "</TD><TD align='right'>";
	echo $value["hat_tricks"];
	echo "</TD></TR>\n";
}
echo "<TABLE>\n";
?>

<BR>
<BR>

<? echo foot_footer($championship, "scorers"); ?>

</BODY>
</HTML>
